<?php 

class Product_model extends CI_Model {
	
    var $_table = 'temp_product';

    function get_all($limit = 0, $offset = 0) {

        $q = $this->db  ->select()
                        ->from($this->_table)
                        ->order_by('p_id DESC')
                        ->limit($limit, $offset)
                        ->get();
        $res['rows'] = $q->result();

        // count
        $q = $this->db  ->select('COUNT(*) as count',FALSE)
                        ->from($this->_table)
                        ->get();                      
        $tmp = $q->row();
        $res['totalRows'] = $tmp->count;
        
        return $res;

    }
   
    function get_by_filter($where = array(), $like = array(), $limit = 0, $offset = 0) {
        
        
        if (count($where) > 0 && $where == true){
            $this->db->where($where);
        }
        if (count($like) > 0 && $like == true){
            $this->db->like($like);
        }
        $this->db   ->select()
                    ->from($this->_table)
                    ->order_by('p_id DESC')
                    ->limit($limit, $offset);
        $q = $this->db->get();
        $res['rows'] = $q->result();
        
        // count
        if (count($where) > 0 && $where == true){
            $this->db->where($where);
        }
        if (count($like) > 0 && $like == true){
            $this->db->like($like);
        }
        $this->db   ->select('COUNT(*) as count',FALSE)
                    ->from($this->_table);
                      
        $q = $this->db->get();                      
        $tmp = $q->row();
        $res['totalRows'] = $tmp->count;

        return $res;  
        
    }


    function get_by_id($id = NULL) {
        
        $q = $this->db  ->select()
                        ->from($this->_table)
                        ->where('p_id', $id)
                        ->get();
        $res = $q->row();
        return $res;
        
    }
    
    function get_all_categories() {
        
        $q = $this->db  ->select()
                        ->from('product_category')
                        ->get();
        $res = $q->result();
        
        return $res;
        
    }
    
    function get_categories_by_type($type) {
        
        $q = $this->db  ->select()
                        ->from('product_category')
                        ->where('status','1')
                        ->like('pcat_code',$type,'after')
                        ->get();
        $res = $q->result();
        
        return $res;
        
    }
    
    function get_brands() {
        
        $q = $this->db  ->select()
                        ->from('brand')
                        ->where('status','1')
                        ->order_by('brand_name')
                        ->get();
        $res = $q->result();

        return $res;
            
    }
    
    function get_materials() {
        
        $q = $this->db  ->select('DISTINCT material')
                        ->from($this->_table)
                        ->where('status','1')
                        ->get();
        $res = $q->result();
        
        return $res;
        
    }
    
    function update_by_id($id,$data) {
        $this->db->where('p_id',$id);
        $this->db->update($this->_table,$data);
        
    }
    
    function add($data) {
        $this->db->insert($this->_table,$data);
    }
         
}